package cn.zzf.algs.note.exercise.fundamental;

import cn.zzf.algs.note.base.collection.v1.LinkedListQueue;
import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * @author GaoFeng
 * @date 2019/11/28 19:39
 */
public class Ex1_3_37 {

    public static void game() {
        LinkedList<Integer> queue = new LinkedList<>();
        LinkedListQueue<Integer> removeQueue = new LinkedListQueue<>();

        int n = 7;
        int m =  2;

        for (int i = 0; i < n; i++) {
            queue.add(i);
        }


        int count = 1;

        while (queue.size() > 0) {
            Iterator<Integer> iterator = queue.iterator();
            while (iterator.hasNext()) {
                Integer next = iterator.next();
                if (count % m == 0) {
                    iterator.remove();
                    removeQueue.enqueue(next);
                }
                count++;
            }
        }

        // 遍历并打印结果
        for (Integer index : removeQueue) {
            StdOut.println(index);
        }

    }

    public static void main(String[] args) {
         game();
    }

}
